﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DeviceInfoManage.DataAccessHelper;
using DeviceInfoManage.DataAccessLayer;
/// <summary>
/// DeviceInfo 的摘要说明
/// </summary>
/// 
namespace DeviceInfoManage.BusinessLogicLayer
{
    /*本业务类对应数据库中的deviceInfo数据表，主要对其进行管理*/
    /*设备信息表
      CREATE TABLE [dbo].[deviceInfo] (
	     [deviceId] [int] IDENTITY (1, 1) NOT NULL ,  				         //设备编号
	     [deviceName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , 	 //设备名称
	     [deviceTypeId] [int] NULL , 						                 //设备类型编号
	     [deviceSign] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , 		 //设备品牌
	     [deviceModel] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,		 //设备型号
	     [deviceSerialNumber] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,//设备序列号
	     [deviceImagePath] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,	 //设备图片路径
	     [deviceState] [smallint] NULL ,						             //设备状态
	     [deviceMadePlace] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,	 //设备生产厂家
	     [deviceOutDate] [datetime] NULL ,					                 //设备出厂日期
	     [devicePurchaseTime] [datetime] NULL ,					             //设备购买日期
	     [deviceMemo] [text] COLLATE Chinese_PRC_CI_AS NULL 			     //设备备注信息
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]*/
    public class DeviceInfo
    {
        #region 字段属性
        private int deviceId;               /*设备编号*/
        private string deviceName;          /*设备名称*/
        private int deviceTypeId;           /*设备类型编号*/
        private string deviceSign;          /*设备品牌*/
        private string deviceModel;         /*设备型号*/
        private string deviceSerialNumber;  /*设备序列号*/
        private string deviceImagePath;     /*设备图片路径*/
        private int deviceState;            /*设备状态*/
        private string deviceNumber;        /*设备资产编号*/
        private DateTime deviceOutDate;     /*设备出厂日期*/
        private DateTime devicePurchaseTime;/*设备购买日期*/
        private string deviceMemo;          /*设备备注信息*/
        private string departmentName;      /*设备使用部门*/
        private string _ipAddress;
        private string _windowNumber;
        public string DepartmentName
        {
            get { return departmentName; }
            set { departmentName = value; }
        }
        private string deviceUser;          /*设备使用人*/

        public string DeviceUser
        {
            get { return deviceUser; }
            set { deviceUser = value; }
        }
        private string deviceCPU;           /*设备CPU*/

        public string DeviceCPU
        {
            get { return deviceCPU; }
            set { deviceCPU = value; }
        }
        private string deviceMemory;        /*设备内存*/

        public string DeviceMemory
        {
            get { return deviceMemory; }
            set { deviceMemory = value; }
        }
        private string deviceHarddisk;      /*设备硬盘*/

        public string DeviceHarddisk
        {
            get { return deviceHarddisk; }
            set { deviceHarddisk = value; }
        }
        private string errMessage;          /*业务操作错误信息*/

        public int DeviceId
        {
            set { this.deviceId = value; }
            get { return this.deviceId; }
        }
        public string DeviceName
        {
            set { this.deviceName = value; }
            get { return this.deviceName; }
        }
        public int DeviceTypeId
        {
            set { this.deviceTypeId = value; }
            get { return this.deviceTypeId; }
        }
        public string DeviceSign
        {
            set { this.deviceSign = value; }
            get { return this.deviceSign; }
        }
        public string DeviceModel
        {
            set { this.deviceModel = value; }
            get { return this.deviceModel; }
        }
        public string DeviceSerialNumber
        {
            set { this.deviceSerialNumber = value; }
            get { return this.deviceSerialNumber; }
        }
        public string DeviceImagePath
        {
            set { this.deviceImagePath = value; }
            get { return this.deviceImagePath; }
        }
        public int DeviceState
        {
            set { this.deviceState = value; }
            get { return this.deviceState; }
        }
        public string DeviceNumber
        {
            set { this.deviceNumber = value; }
            get { return this.deviceNumber; }
        }
        public DateTime DeviceOutDate
        {
            set { this.deviceOutDate = value; }
            get { return this.deviceOutDate; }
        }
        public DateTime DevicePurchaseTime
        {
            set { this.devicePurchaseTime = value; }
            get { return this.devicePurchaseTime; }
        }
        public string DeviceMemo
        {
            set { this.deviceMemo = value; }
            get { return this.deviceMemo; }
        }
        public string ErrMessage
        {
            set { this.errMessage = value; }
            get { return this.errMessage; }
        }
        /// <summary>
        /// IP地址
        /// </summary>
        public string IPAddress
        {
            get { return _ipAddress; }
            set { _ipAddress = value; }
        }
        /// <summary>
        /// 窗口号
        /// </summary>
        public string WindowNumber
        {
            get { return _windowNumber; }
            set { _windowNumber = value; }
        }
        #endregion

        #region 构造函数
        /*构造函数，对各个字段进行初始化*/
        public DeviceInfo()
        {
            deviceId = 0;
            deviceName = "";
            deviceTypeId = 0;
            deviceSign = "";
            deviceModel = "";
            deviceSerialNumber = "";
            deviceImagePath = "";
            deviceState = 1; /*1代表该设备处于空闲状态*/
            deviceNumber = "";
            deviceOutDate = DateTime.MinValue;
            devicePurchaseTime = DateTime.MinValue;
            deviceMemo = "";
            errMessage = "";
        }
        #endregion


        /*向数据库中加入新设备，保存该设备的各个字段信息*/
        public bool NewDeviceAdd()
        {
            /*首先检查数据库中是否已经存在了该设备*/
            //if (IsExist(this.deviceNumber))
            //{
            //    this.errMessage = "数据库中已经存在该【资产编号】的设备!";
            //    return false;
            //}
            /*然后构造插入sql语句执行新设备的加入*/
            string insertString = "insert into deviceInfo(deviceName,deviceTypeId,deviceSign,deviceModel,deviceSerialNumber,deviceState,IPAddress,WindowNumber,departmentName,deviceMemo,deviceUser,deviceCPU,deviceMemory,deviceHarddisk) values (";
            insertString += SqlString.GetQuotedString(deviceName) + ",";
            insertString += deviceTypeId + ",";
            insertString += SqlString.GetQuotedString(deviceSign) + ",";
            insertString += SqlString.GetQuotedString(deviceModel) + ",";
            insertString += SqlString.GetQuotedString(deviceSerialNumber) + ",";
            //insertString += SqlString.GetQuotedString(deviceImagePath) + ",";
            insertString += deviceState + ",";
            insertString += SqlString.GetQuotedString(IPAddress) + ",";
            insertString += SqlString.GetQuotedString(WindowNumber) + ",";
            insertString += SqlString.GetQuotedString(departmentName) + ",";
            insertString += SqlString.GetQuotedString(deviceMemo) + ",";
            insertString += SqlString.GetQuotedString(deviceUser) + ",";
            insertString += SqlString.GetQuotedString(deviceCPU) + ",";
            insertString += SqlString.GetQuotedString(deviceMemory) + ",";
            insertString += SqlString.GetQuotedString(deviceHarddisk) + ")";
            /*调用数据层InsertOrUpdate函数返回数据库中受影响的行数，如果成功是大于0的*/
            DataBase db = new DataBase();
            if (db.InsertOrUpdate(insertString) < 0)
            {
                this.errMessage = "新设备加入系统时发生了错误!";
                return false;
            }
            return true;
        }

        /*根据设备编号得到设备各个字段信息*/
        public bool GetDeviceInfo(int deviceId)
        {
            string queryString = "select * from deviceInfo where deviceId =" + deviceId;
            DataBase db = new DataBase();
            DataSet ds = db.GetDataSet(queryString);
            if (ds.Tables[0].Rows.Count == 0)
            {
                this.errMessage = "不存在该设备的信息！";
                return false;
            }
            /*将查询到的记录的各个字段信息保存在本类相应字段中*/
            DataRow dr = ds.Tables[0].Rows[0];
            this.deviceId = deviceId;
            this.deviceName = dr["deviceName"].ToString();
            this.deviceTypeId = Int32.Parse(dr["deviceTypeId"].ToString());
            this.deviceSign = dr["deviceSign"].ToString();
            this.deviceModel = dr["deviceModel"].ToString();
            this.deviceSerialNumber = dr["deviceSerialNumber"].ToString();
            this.deviceImagePath = dr["deviceImagePath"].ToString();
            this.deviceState = Int32.Parse(dr["deviceState"].ToString());
            this.deviceNumber = dr["deviceNumber"].ToString();
            this.departmentName = dr["departmentName"].ToString();
            this.deviceUser = dr["deviceUser"].ToString();
            this.deviceCPU = dr["deviceCPU"].ToString();
            this.deviceHarddisk = dr["deviceHarddisk"].ToString();
            this.deviceMemory = dr["deviceMemory"].ToString();
            this.deviceMemo = dr["deviceMemo"].ToString();
            this.IPAddress = dr["IPAddress"].ToString();
            this.WindowNumber = dr["WindowNumber"].ToString();
            return true;
        }

        /*根据设备编号更新设备的信息，设备编号参数已经传入本类的私有字段中*/
        public bool DeviceInfoUpdate(string rootPath)
        {
            ///*得到该设备原来的图片路径,如果新的图片路径跟原来不一样，需要删除老图片*/
            //string deviceOldImagePath = this.GetDeviceImagePath(this.deviceId);
            ///*如果原来已经存在了设备图片*/
            //if (deviceOldImagePath != "")
            //{
            //    /*如果图片路径不一样，说明管理员选择了新的图片，此时需要删出原来的图片*/
            //    if (String.Compare(deviceOldImagePath, this.deviceImagePath, true) != 0)
            //        System.IO.File.Delete(rootPath + "\\" + deviceOldImagePath);
            //}
            /*构造更新sql语句然后执行更新操作,要更新的设备信息已经存在于本类相应字段中*/
            string updateString = "update deviceInfo set deviceName=" + SqlString.GetQuotedString(this.deviceName);
            updateString += ",deviceTypeId=" + this.deviceTypeId;
            updateString += ",deviceSign=" + SqlString.GetQuotedString(this.deviceSign);
            updateString += ",deviceModel=" + SqlString.GetQuotedString(this.deviceModel);
            updateString += ",deviceSerialNumber=" + SqlString.GetQuotedString(this.deviceSerialNumber);
            //updateString += ",deviceImagePath=" + SqlString.GetQuotedString(this.deviceImagePath);
            updateString += ",deviceNumber=" + SqlString.GetQuotedString(this.deviceNumber);
            updateString += ",IPAddress=" + SqlString.GetQuotedString(this.IPAddress);
            updateString += ",WindowNumber=" + SqlString.GetQuotedString(this.WindowNumber);
            updateString += ",deviceUser=" + SqlString.GetQuotedString(this.deviceUser);
            updateString += ",departmentName=" + SqlString.GetQuotedString(this.departmentName);
            updateString += ",deviceCPU=" + SqlString.GetQuotedString(this.deviceCPU);
            updateString += ",deviceMemory=" + SqlString.GetQuotedString(this.deviceMemory);
            updateString += ",deviceHarddisk=" + SqlString.GetQuotedString(this.deviceHarddisk);
            updateString += ",deviceMemo=" + SqlString.GetQuotedString(this.deviceMemo);
            updateString += " where deviceId=" + this.deviceId;
            /*调用数据层代码实行更新，如果成功受影响的行数大于0*/
            DataBase db = new DataBase();
            if (db.InsertOrUpdate(updateString) < 0)
            {
                this.errMessage = "更新设备信息时发生了错误!";
                return false;
            }
            return true;
        }

        /*检查数据库中是否已经存在了该设备名称*/
        public bool IsExist(string deviceNumber)
        {
            DataBase db = new DataBase();
            string queryString = "select * from deviceInfo where deviceNumber=" + SqlString.GetQuotedString(deviceNumber);
            return db.GetRecord(queryString);
        }

        /*检查数据库中是否已经存在了该编号的设备*/
        public bool IsExist(int deviceId)
        {
            DataBase db = new DataBase();
            string queryString = "select * from deviceInfo where deviceId=" + deviceId;
            return db.GetRecord(queryString);
        }

        /*得到某个设备编号的设备状态编号*/
        public int GetDeviceState(int deviceId)
        {
            string queryString = "select deviceState from deviceInfo where deviceId=" + deviceId;
            DataSet ds = (new DataBase()).GetDataSet(queryString);
            if (ds.Tables[0].Rows.Count == 0) return 0;
            return Int32.Parse(ds.Tables[0].Rows[0]["deviceState"].ToString());
        }

        /*根据设备编号得到图片路径字符串*/
        public string GetDeviceImagePath(int deviceId)
        {
            string queryString = "select deviceImagePath from deviceInfo where deviceId = " + deviceId;
            DataSet ds = (new DataBase()).GetDataSet(queryString);
            if (ds.Tables[0].Rows.Count == 0) return String.Empty;
            return ds.Tables[0].Rows[0]["deviceImagePath"].ToString();
        }

        /*根据查询条件对设备信息进行查询*/
        //public DataSet QueryDevice(string deviceName, string deviceType, string startTimeString, string endTimeString)
        //{
        //    try
        //    {
        //        string queryString = "select * from deviceInfoView where deviceName like '%";
        //        queryString += deviceName + "%' and deviceTypeName like '%";
        //        queryString += deviceType + "%'";
        //        /*如果用户设备了开始时间的查询条件*/
        //        if (startTimeString != "")
        //            queryString += " and devicePurchaseTime>'" + Convert.ToDateTime(startTimeString) + "'";
        //        /*如果用户设置了结束时间的查询条件*/
        //        if (endTimeString != "")
        //            queryString += " and devicePurchaseTIme<'" + Convert.ToDateTime(endTimeString) + "'";
        //        DataBase db = new DataBase();
        //        return db.GetDataSet(queryString);
        //    }
        //    catch (Exception ex)
        //    {
        //        this.errMessage = ex.Message;
        //        return null;
        //    }
        //}

        public DataSet QueryDevice(string WindowNumber, string deviceType, string departmentName, string IPAddress, string DeviceDemo)
        {
            try
            {
                string queryString = "select * from deviceInfoView";
                string strSql = string.Empty;
                if (WindowNumber != "") strSql += " and WindowNumber = '" + WindowNumber + "'";
                if (deviceType != "") strSql += " and deviceTypeName like '%" + deviceType + "%'";
                if (departmentName != "") strSql += " and departmentName like '%" + departmentName + "%'";
                if (IPAddress != "") strSql += " and IPAddress = '" + IPAddress + "'";
                if (DeviceDemo != "") strSql += " and deviceMemo like '%" + DeviceDemo + "%'";
                if (strSql.StartsWith(" and"))
                {
                    strSql = strSql.Remove(0, 4);
                    queryString += " where" + strSql;
                }
                ///*如果用户设备了开始时间的查询条件*/
                //if (startTimeString != "")
                //    queryString += " and devicePurchaseTime>'" + Convert.ToDateTime(startTimeString) + "'";
                ///*如果用户设置了结束时间的查询条件*/
                //if (endTimeString != "")
                //    queryString += " and devicePurchaseTIme<'" + Convert.ToDateTime(endTimeString) + "'";

                DataBase db = new DataBase();
                return db.GetDataSet(queryString);
            }
            catch (Exception ex)
            {
                this.errMessage = ex.Message;
                return null;
            }
        }
        /*查询某个类别的处于空闲状态的设备信息*/
        public DataSet QueryDevice(int deviceTypeId)
        {
            string queryString = "select deviceId,deviceName from deviceInfo where deviceState=1 and deviceTypeId=" + deviceTypeId;
            DataSet ds = (new DataBase()).GetDataSet(queryString);
            return ds;
        }
        public DataSet GetDevice()
        {
            string queryString = "SELECT [deviceSerialNumber], [deviceName], [departmentName], [deviceTypeName], [deviceModel],[deviceId],[IPAddress],[WindowNumber]  FROM [deviceInfoView]";
            DataSet ds = (new DataBase()).GetDataSet(queryString);
            return ds;
        }
        /*根据设备编号得到设备的名称*/
        public string GetDeviceName(int deviceId)
        {
            string queryString = "select deviceName from deviceInfo where deviceId = " + deviceId;
            DataSet ds = (new DataBase()).GetDataSet(queryString);
            return ds.Tables[0].Rows[0]["deviceName"].ToString();
        }

        /// <summary>
        /// 删除设备信息
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public string DelDeviceById(string deviceId)
        {
            string strSql = "DELETE [deviceInfo] WHERE [deviceId] =" + deviceId;
            int result = (new DataBase()).InsertOrUpdate(strSql);
            if (result <= 0)
            {
                return "error!";
            }
            else
            {
                return "ok!";
            }
        }
    }
}

